import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  EventEmitter,
  Input,
  OnInit,
  Output,
  TemplateRef,
  ViewChild
} from '@angular/core';
import {PageHeaderType} from '../../../../share/components/page-header/page-header.component';
import {MyTableConfig} from '../../../../share/components/ant-table/ant-table.component';
import {NzTableQueryParams} from 'ng-zorro-antd/table';
import {SearchCommonVO} from '../../../../core/services/types';
import {ExamsManageService, TestUserDetail, TestUserList} from '../../../../core/services/http/exams-manage/exams-manage.service';
import {finalize} from 'rxjs/operators';


interface SearchParam {
  userName: string;
}

@Component({
  selector: 'app-detail-exams',
  templateUrl: './detail-exams.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DetailExamsComponent implements OnInit {
  @Output() returnBack: EventEmitter<any>;
  @ViewChild('operationTpl', {static: true}) operationTpl!: TemplateRef<any>;
  searchParam: Partial<SearchParam> = {};
  @Input() currentPageNum!: number;
  tableConfig!: MyTableConfig;
  @Input() id!: number;
  pageHeaderInfo: Partial<PageHeaderType> = {
    title: '考试人员列表',
    breadcrumb: ['考试教育', '考试人员列表']
  };
  dataList: TestUserList[] = [];
  userId!: number;
  isVisible = false;
  isConfirmLoading = false;
  dataInfom: TestUserDetail[] = [];

  constructor(private dataService: ExamsManageService, private cdr: ChangeDetectorRef) {
    this.returnBack = new EventEmitter<any>();
  }

  changePageSize(e: number): void {
    this.tableConfig.pageSize = e;
  }

  /*重置*/
  resetForm(): void {
    this.searchParam = {};
    this.getDataList();
  }

  returnToList(): void {
    this.returnBack.emit({refesh: false, pageNo: this.currentPageNum});
  }

  // 触发表格变更检测
  tableChangeDectction(): void {
    // 改变引用触发变更检测。
    this.dataList = [...this.dataList];
    this.cdr.detectChanges();
  }

  tableLoading(isLoading: boolean): void {
    this.tableConfig.loading = isLoading;
    this.tableChangeDectction();
  }

  showModal(userIds: number): void {
    this.isVisible = true;
    this.dataInfom.length = 0;
    this.dataService.getUserExamsDetail({userId: userIds, questionTestId: this.id}).subscribe(obe => {
      obe.forEach(item => {
        this.dataInfom.push(item);
      });
    });
  }

  handleOk(): void {
    this.isConfirmLoading = true;
    setTimeout(() => {
      this.isVisible = false;
      this.isConfirmLoading = false;
    }, 1000);
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  getDataList(e?: NzTableQueryParams): void {
    this.tableConfig.loading = false;
    const params: SearchCommonVO<any> = {
        pageSize: this.tableConfig.pageSize!,
        pageNum: e?.pageIndex || this.tableConfig.pageIndex!,
        filters: this.searchParam,
        questionTestId: this.id
      }
    ;
    this.dataService.userExamsDetailList(params).pipe(finalize(() => {
      this.tableLoading(false);
    })).subscribe((data => {
      const {list, total, pageNum} = data;
      this.dataList = [...list];
      this.tableConfig.total = total!;
      this.tableConfig.pageIndex = pageNum!;
      this.tableLoading(false);
    }), error => this.tableLoading(false));
  }

  ngOnInit(): void {
    this.initTable();
  }

  private initTable(): void {
    this.tableConfig = {
      showCheckbox: false,
      headers: [
        {
          title: '人员',
          field: 'userName',
          width: 60
        },
        {
          title: '考试次数',
          field: 'countTest',
          width: 60
        },
        {
          title: '最高分',
          field: 'maxScore',
          width: 60
        },
        {
          title: '是否通过',
          field: 'passFlag',
          pipe: 'passFlag',
          width: 60
        },
        {
          title: '最后考试时间',
          field: 'lastTestTime',
          pipe: 'date:yyyy-MM-dd HH:mm',
          width: 100,
        },
        {
          title: '操作',
          tdTemplate: this.operationTpl,
          width: 50,
          fixed: true
        }
      ],
      total: 0,
      loading: true,
      pageSize: 10,
      pageIndex: 1,
    };
  }

}
